import ijeoma.tween.*;
import ijeoma.easing.*;

PFont font;
int fontSize = 16;

Tween[] tweens;
TweenGroup tweenGroup;

int barCount = 10;
float barWidth = 0;
float barHeightMin;
float barHeightMax;
int barTopMargin = 24;

float  duration = 100;
String durationMode = Tween.FRAMES;

String easing = Tween.QUAD_EASE_IN;

void setup() {
  size(400, 200, P3D);

  noStroke();

  font = loadFont("Ziggurat-HTF-Black-32.vlw");
  textFont(font, fontSize);

  barWidth = width / barCount;
  barHeightMax = height;
  barHeightMin = height / 5;

  tweens = new Tween[barCount];

  for (int i = 0; i < barCount; i++) {
    tweens[i] = new Tween(this, "Tween" + i, 0f, random(barHeightMin, barHeightMax), duration, durationMode, 0f, easing);
  }

  tweenGroup = new TweenGroup(this, tweens);
  tweenGroup.start();
}

void draw() {
  background(255);

  tweenGroup.update();

  for (int i = 0; i < barCount; i++) {
    Tween t = ((Tween) tweenGroup.tweens.get(i));

    float h = t.position;
    float r = map(h, barHeightMax/4, barHeightMax, 0, 255);
    String p = (int) h +"";
    
    fill(color(r, 0, 0));
    rect(i * barWidth, height, barWidth, -h);
    
    fill(255);
    text(p, i * barWidth + (barWidth - textWidth(p))/2, height - (h - barTopMargin));
  }
}

void tweenStartedEvent(Tween _t) {
  // println(_t + " started");
}

void tweenEndedEvent(Tween _t) {
  // println(_t + " ended");
}

void tweenGroupStartedEvent(TweenGroup _tg) {
  // println(_tg + " started");
}

void tweenGroupEndedEvent(TweenGroup _tg) {
  // println(_tg + " ended");
  for (int i = 0; i < _tg.tweens.size(); i++) {
    Tween t1 = (Tween) _tg.tweens.get(i);
    Tween t2 = new Tween(this, "Tween" + i, t1.end, random(barHeightMin, barHeightMax), duration, durationMode, 100f, easing);
    t2.addEventListener(_tg);
    _tg.tweens.set(i, t2);
  }

  _tg.start();
}


